﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LongShare.UCAdapter.IDAL;
using LongShare.UCAdapter;
using System.Xml;

namespace MvcProject.Utils
{
    public class UC
    {
        public static string GetOrganizeXml(string organizeId)
        {
            string stringXml = null;
            IOrganizationQuery org = PubFactory.GetOrganizationQuery();
            XmlNode rootNode = org.GetOrgTree(organizeId);
            if (rootNode != null && rootNode.HasChildNodes)
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml("<tree id=\"0\"></tree>");
                XmlNode rootXmlTree = xml.DocumentElement;

                CreateTreeNode(rootXmlTree, rootNode.SelectSingleNode("//OrgUnits"));

                stringXml = rootXmlTree.OuterXml;
            }
            return stringXml;
        }

        //创建树形结构
        private static void CreateTreeNode(XmlNode xmlTree, XmlNode xmlnode)
        {
            foreach (XmlNode node in xmlnode.ChildNodes)
            {
                if (node.Name == "OrgUnit")
                {
                    XmlElement itemNode = xmlTree.OwnerDocument.CreateElement("item");
                    string name = node.SelectSingleNode("descendant::name").InnerText;
                    if (!name.EndsWith("公司")) continue;
                    itemNode.SetAttribute("id", node.SelectSingleNode("descendant::organId").InnerText);
                    itemNode.SetAttribute("text", name);
                    //itemNode.SetAttribute("sortnumber", node.SelectSingleNode("descendant::sortNumber").InnerText);
                    itemNode.SetAttribute("im0", "book.gif");
                    itemNode.SetAttribute("im1", "books_open.gif");
                    itemNode.SetAttribute("im2", "books_close.gif");

                    xmlTree.AppendChild(itemNode);
                    CreateTreeNode(itemNode, node);
                }
            }
        }
        public static string TotalUnitTreeXml()
        {
            string tree = GetOrganizeXml("OD000001");
            return tree;
        }
        public static string SubUnitTreeXml()
        {
            string tree = GetOrganizeXml("OD000002");
            return tree;
        }
    }
}